/*
 * Rhy RSS
 * 
 * @author Tristan Maindron
 * @dependencies rhy.core.ajax
 * 
 */

Rhy.extra.rss = {
    channels: [],
    channelsMap: [],
    loadQueue: [],
    synchronous: true,
    loading: false,
    
    onReady: function(){},
    
    reset: function(){
        Rhy.extra.rss.channelsMap = [];
    },
    
    load: function(url, id, callback){
        
        if(typeof(callback) == 'undefined'){
            callback = function(){};
        }
        
        Rhy.extra.rss.loadQueue.push({
            url: url, 
            id: id,
            callback: callback
        });
        
        if(!Rhy.extra.rss.loading || !Rhy.extra.rss.synchronous){
            Rhy.extra.rss.startRequest();
        }
    },
    
    startRequest: function(){
        Rhy.extra.rss.loading = true;
        var o = Rhy.extra.rss.loadQueue.shift();
        
        Rhy.ajax({
          url: o.url,
          type: 'xml',
          success: Rhy.extra.rss.handleSuccess(o.callback, o.id)
        });
    },
    
    filter: function(text){
        //text = text.replace(/<img\/?[^>]+(>|$)/g, "");
        //text = text.replace(/<a\/?[^>]+(>|$)/g, "");
        return text;
    },
    
    parseRSS: function(rss){
        var elements = [];
        
        for(var j = 0; j < rss.length; j++){
            if(typeof(rss[j].getElementsByTagName('title')[0].firstChild.nodeValue) != 'undefined'){

                var description = "";

                if(typeof(rss[j].getElementsByTagName('description')[0].firstChild) != 'undefined'
                        && rss[j].getElementsByTagName('description')[0].firstChild != null){
                            description = $r.ex.rss.filter(rss[j].getElementsByTagName('description')[0].firstChild.nodeValue);
                        }

                elements.push({
                    title: rss[j].getElementsByTagName('title')[0].firstChild.nodeValue,
                    link: rss[j].getElementsByTagName('link')[0].firstChild.nodeValue,
                    description: description
                });
            }
        }
        
        return elements;
    },
    
    parseAtom: function(rss){
        var elements = [];
        
        var entryLabel = 'summary';
        
        if(rss[0].getElementsByTagName(entryLabel).length == 0){
            entryLabel = 'content';
        }
        
        for(var j = 0; j < rss.length; j++){

                if(typeof(rss[j].getElementsByTagName(entryLabel)[0].firstChild) != 'undefined'
                        && rss[j].getElementsByTagName(entryLabel)[0].firstChild != null){
                    elements.push({
                        title: rss[j].getElementsByTagName('title')[0].firstChild.nodeValue,
                        link: rss[j].getElementsByTagName('link')[0].getAttribute('href'),
                        description: $r.ex.rss.filter(rss[j].getElementsByTagName(entryLabel)[0].firstChild.nodeValue)
                    });
                }
        }
        
        return elements;
    },
    
    handleSuccess: function(callback, id){
        return function(o){
            var channels = o.responseXML.getElementsByTagName('channel');
            
            if(channels.length == 0){
                // Sometimes there are no channels. So let emulate one.
                channels = [ o.responseXML ];
            }
            
            for(var i = 0; i < channels.length; i++){

                var elements;
                
                // By default try to use default RSS format
                var elementsData = channels[i].getElementsByTagName('item');
                
                if(elementsData.length == 0){
                    // If not working, use Atom format
                    elementsData = channels[i].getElementsByTagName('entry');
                    elements = $r.ex.rss.parseAtom(elementsData);
                }
                else{
                    elements = $r.ex.rss.parseRSS(elementsData);
                }
                
                

                Rhy.extra.rss.channels.push ({
                    title: channels[i].getElementsByTagName('title')[0].textContent,
                    content: elements
                });

                Rhy.extra.rss.channelsMap[id] = {
                    title: channels[i].getElementsByTagName('title')[0].textContent,
                    content: elements
                };
            }

            callback(o);

           Rhy.extra.rss.loading = false;
           if(Rhy.extra.rss.loadQueue.length > 0)
               Rhy.extra.rss.startRequest();
           else{
               Rhy.extra.rss.onReady();
           }
        }
    }
}